using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Collections;

namespace _3DWorldOfSmith
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        GraphicsDevice device;

        Matrix world;
        Matrix projection;
        Matrix view;

        Square floor;
        Texture2D floorTexture;

        Cube cube;
        Texture2D cubeTexture;

        Cube windmillBody;
        Texture2D windmillBodyTexture;
        WindmillFans fans;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            this.world = Matrix.Identity;
            this.projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                                                                    Window.ClientBounds.Width /
                                                                    (float)Window.ClientBounds.Height,
                                                                    1, 100);
            this.view = Matrix.CreateLookAt(new Vector3(0, 0, -10), Vector3.Zero, Vector3.Up);

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            device = graphics.GraphicsDevice;

            floorTexture = Content.Load<Texture2D>(@"Textures/grass");
            cubeTexture = Content.Load<Texture2D>(@"Textures/bricks");
            windmillBodyTexture = Content.Load<Texture2D>(@"Textures/windmill");

            Instantiate();
        }

        public void Instantiate()
        {
            // Cube instantiation and properties definition
            cube = new Cube(device);
            cube.texture = cubeTexture;
            cube.world = world;
            cube.projection = projection;
            cube.view = view;
            cube.position = new Vector3(3, 1, 3);
            cube.rotation = new Vector3(0, 0, 0);
            cube.size = new Vector3(1, 1, 1);

            // Floor instantiation and properties definition
            floor = new Square(device);
            floor.texture = floorTexture;
            floor.world = world;
            floor.projection = projection;
            floor.view = view;
            floor.position = new Vector3(0, 0, 0);
            floor.rotation = new Vector3(1.5f, 0, 0);
            floor.size = new Vector3(25, 25, 0);

            // Windmill's body instantiation and properties definition
            windmillBody = new Cube(device);
            windmillBody.world = world;
            windmillBody.projection = projection;
            windmillBody.view = view;
            windmillBody.position = new Vector3(0, 0, 0);
            windmillBody.rotation = new Vector3(0, 0, 0);
            windmillBody.size = new Vector3(0, 0, 0);

            // Windmill's body instantiation and properties definition
            fans = new WindmillFans(device);
            fans.world = world;
            fans.projection = projection;
            fans.view = view;
            fans.position = new Vector3(0, 0, -1);
            fans.rotation = new Vector3(0, 0, 0);
            fans.size = new Vector3(1, 1, 1);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            cube.Update();
            floor.Update();
            windmillBody.Update();
            fans.Update();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            device.RasterizerState = RasterizerState.CullNone;
            
            cube.Draw();
            floor.Draw();
            windmillBody.Draw();
            fans.Draw();

            base.Draw(gameTime);
        }
    }
}